#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# data time : 2022/12/5   4:07 下午
# project   : dev_log.py
# IDE       : PyCharm
# email     :17600960626@163.com




import logging
import os
import shutil
import subprocess
import time
from lib.read_sql import db

paths= '/Users/xm123/PycharmProjects/KeyboardEfficiencyTest-Tools/report/'

class DEVS():
    def __init__(self):
        self.device_list = []
        device_str = subprocess.getoutput('adb devices')
        m = (device_str.strip()).split('\n')
        for line in m[1:]:
            if 'List' not in line:
                self.device_list.append(line.split('\t')[0])


    def sql_dev(self):
        self.dev_numb = []
        for i in self.device_list:
            sql2 = 'SELECT devices,numbers FROM master_table where devices="%s" AND numbers is NOT NULL ' %i
            try:
                res = db.selectDb(sql2)
                self.dev_numb.append(res[1][0][1])
            except Exception as e:
                logging.error('报错了%s' % e)
        return self.dev_numb




    def cmd_log(self):
        '''导出logcat到本地'''
        for devid, j in zip(self.device_list, self.dev_numb):
            cmd = 'adb -s %s logcat -d | grep Xposed_hack_module > %s%s-%s-log.txt' % (devid, paths, devid, j)
            ress = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                    universal_newlines=True)
            time.sleep(1)
            ress.kill()


    def rm_log(self):
        '''清空report'''
        shutil.rmtree(paths)
        os.mkdir(paths)

log_cat=DEVS()